﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BOSS.Framework.Search.Result
{
    /// <summary>
    /// 基于字典的结果条目类
    /// </summary>
    public class DictionaryResultItem : AbstractResultItem
    {
        private IDictionary<string, object> m_dic;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="id"></param>
        /// <param name="score"></param>
        /// <param name="title"></param>
        public DictionaryResultItem(int id, float score, string title)
            : base(id, score, title)
        {
            this.m_dic = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="id"></param>
        /// <param name="score"></param>
        /// <param name="title"></param>
        /// <param name="dic"></param>
        public DictionaryResultItem(int id, float score, string title, IDictionary<string, object> dic)
            : base(id, score, title)
        {
            this.m_dic = dic;
        }

        #region IResultItem Members

        /// <summary>
        /// 获取 条目的字段名称集合<br/>
        /// </summary>
        public override IEnumerable<string> FieldNames
        {
            get
            {
                return this.m_dic.Keys;
            }
        }

        /// <summary>
        /// 获取 底层对象
        /// </summary>
        public override object UnderlayerObject
        {
            get
            {
                return this.m_dic;
            }
        }

        /// <summary>
        /// 获取值
        /// </summary>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public override object GetValue(string fieldName)
        {
            if (this.m_dic.ContainsKey(fieldName))
            {
                return this.m_dic[fieldName];
            }
            return null;
        }

        #endregion

        /// <summary>
        /// 添加当前结果的字段
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public void AddField(string name, object value)
        {
            this.m_dic[name] = value;
        }

        /// <summary>
        /// 添加当前结果的字段（仅当该字段不存在时）
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public void AddFieldIfNotExist(string name, object value)
        {
            if (!this.m_dic.ContainsKey(name))
            {
                this.m_dic.Add(name, value);
            }
        }
    }
}
